package com.example.mybatis.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.mybatis.form.UserForm;
import com.example.mybatis.model.ServiceResponse;
import com.example.mybatis.model.SheetInfo;
import com.example.mybatis.model.UserTO;
import com.example.mybatis.service.IExcelService;
import com.example.mybatis.utils.AnnotationSheetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;


@RestController
public class ExcelController {
    @Autowired
    private IExcelService excelService;

    private  static final Logger logger = LoggerFactory.getLogger(ExcelController.class);

    public static SheetInfo searchUserSheet = null;

    /**
     * 导出Excel文件接口,带条件查询的导出每次只能导出一张表
     * @param sheetName 配置文件中的sheet名
     * @param response 响应
     * @return
     */
    @RequestMapping(value="/exportExcel",method = RequestMethod.GET)
    public void exportExcel(@RequestParam("sheetName") String sheetName,HttpServletResponse response){
        logger.info("sheetName={}",sheetName);
        excelService.exportSheet(sheetName,1,20,response);
    }

    @RequestMapping(value = "export/searchExcel",method = RequestMethod.POST)
    public void exportSearchExcel(UserForm form, HttpServletResponse response)throws Exception{
        logger.info("form={}", JSONArray.toJSONString(form));
        if(searchUserSheet==null){
            searchUserSheet = AnnotationSheetUtil.initSheetInfo(UserTO.class);
        }
        excelService.exportSearchSheet(form,searchUserSheet,response);

    }

    @RequestMapping(value="/importExcel",method = RequestMethod.POST)
    public ServiceResponse<String> importExcel(MultipartFile uploadFile, HttpServletRequest request)throws Exception{
        String requestUrl = request.getRequestURI();
        logger.info("requestUrl={}",requestUrl);
        String fileName = uploadFile.getOriginalFilename();
        logger.info("fileName={}",fileName);
        //先要实现文件上传到服务器
        String workDir = System.getProperty("user.dir");
        String user = (String) request.getSession().getAttribute("userName");
        if(user==null){
            user = "system";
        }
        String saveDir = workDir+"/src/main/resources/static/upload/"+user;
        String savePath = saveDir;
        File folder = new File(savePath);
        if(!folder.isDirectory()){
            folder.mkdirs();
        }
        String filename = uploadFile.getOriginalFilename();

            uploadFile.transferTo(new File(folder,filename));
            StringBuilder builder = new StringBuilder(savePath);
            builder.append('/').append(filename);
            String filePath = builder.toString();
            File excelFile = new File(filePath);
            boolean isExcel2003 = false;
            if(fileName.endsWith(".xls")){
                isExcel2003 = true;
            }
            return excelService.importExcel(excelFile,isExcel2003);

    }

}
